# Author: didiplus
# Date: 2025-01-22 15:45:07
# LastEditTime: 2025-01-22 15:45:10
# LastEditors: didiplus
# Description: GIF验证码类
# FilePath: \pythonscript\Captcha\utls\GifCaptcha.py

import io
import base64
from .TextCaptcha import TextCaptcha

class GifCaptcha(TextCaptcha):
    """动态 GIF 验证码"""

    def create_gif(self) -> bytes:
        """生成动态 GIF 验证码"""
        if self.chars is None:
            self.generate()

        frames = []
        for _ in range(5):  # 生成多个帧
            image = self.create_image()
            frames.append(image)

        output = io.BytesIO()
        frames[0].save(output, format="GIF", save_all=True, append_images=frames[1:], loop=0, duration=100)
        return output.getvalue()

    def to_base64_gif(self) -> str:
        """将动态 GIF 转换为 Base64"""
        gif_data = self.create_gif()
        base64_data = base64.b64encode(gif_data).decode("utf-8")
        return f"data:image/gif;base64,{base64_data}"